<?php

namespace app\service;

use app\model\OrderDeal;
use app\model\OrderDealChargeback;
use app\model\PublicCollect;
use app\model\PublicSaleRule;
use app\model\SystemCertificate;
use app\stateData;
use think\facade\Cache;
use think\facade\Db;

class Publicity
{

    /**
     * 公示栏
     */
    public function setPublicityPersonData($data,$uid)
    {

        $cert = new Certificate();
        $collect = new PublicCollect();
//        $result = $cert->setCertificateDetails ($data);
        $suchData = [];
        $result = [];
        foreach ($data as $index => $item) {
            //如果人数为0 则不显示
            if (isset($item['people_num'])){
                if ($item['people_num'] == 0){
                    unset($data[$index]);
                    continue;
                }
            }
            //公示栏企业资料部与公示栏
            if(isset($item['type'])){
                if ($item['type'] == 3){
                    $item['type'] = [1,2];
                }else{
                    $item['type'] = [$item['type']];
                }
            }

            //特价证书未通过 则不显示
            if (isset($item['certificate_characteristic_status']['id'])){
                if ($item['certificate_characteristic_status']['id'] != 1){
                    $item['certificate_characteristic'] = '';
                }
            }


            $arr = '';
             if (!empty($item['certificate_leixing'])) $arr .= $item['certificate_leixing'].'、';
             if (!empty($item['certificate_leibie'])) $arr .= $item['certificate_leibie'].'、';
             if (!empty($item['certificate_dengji'])) $arr .= $item['certificate_dengji'].'、';
             if (!empty($item['certificate_zhuanye'])) $arr .= $item['certificate_zhuanye'].'、';

             if (!empty($item['certificate_b']['id'])){
                 $arr .= stateData::$_b_doc[$item['certificate_b']['id']].'、';
             }

             if (!empty($item['social_security']['id'])){
                 $arr .= stateData::$_social_security[$item['social_security']['id']].'、';
             }
             //证书id处理
            if (!empty($item['certificate_id'])){
                if (!is_array ($item['certificate_id'])){
                    $item['certificate_id'] =$item['certificate_id'] ? explode(',',$item['certificate_id']) :[];
                }
                $item['certificate_id'] = array_map(function($value){
                    return intval($value);
                },$item['certificate_id']);
            }

             //如果周期text为空的话 返回周期id数组设置为空 否则将字符串改为数组
            if(!empty($item['cycle']['id'])){
                if (!is_array ($item['cycle']['id'])){

                    $item['cycle']['id'] = $item['cycle']['id'] ? explode(',',$item['cycle']['id']) :[];

                    $item['cycle']['id'] = array_map(function($value){
                        return intval($value);
                    },$item['cycle']['id']);
                    if($item['cycle']['text']){
                        $arr .= $item['cycle']['text'].'、';
                    }

                }else{
                    if($item['cycle']['text']){
                        $arr .= $item['cycle']['text'].'、';
                    }
                }

            }

            //项目
            if (!empty($item['project']['id'])){
                $arr .= stateData::$_project[$item['project']['id']].'、';
            }

            if (array_key_exists('transfer',$item)){

                if(!empty($item['transfer']['id'])){
                    $arr .= stateData::$_transfer[$item['transfer']['id']].'、';
                }
            }

            if (!empty($item['contract_price'])) $arr .= $item['contract_price'].'、';
            //判断是否被收藏 1未收藏  2已被别人收藏  3自己收藏
           $isCollect = $collect->getIdCollectModel($item['id']);

            //单位价
            if (isset($item['price_unit'])){
                if (!empty($item['price_unit'])){
                    $arr .= $item['price_unit'].'、';
                }
            }


            if (!empty($isCollect['id'])){
                if ($isCollect['uid'] == $uid){
                    $item['is_sc'] = 3;
                }else{
                    $item['is_sc'] = 2;
                }
            }else{
                $item['is_sc'] = 1;
            }
//
            if (isset($item['sale_uid'])){
                if (!empty($item['sale_uid'])){
                    if($item['sale_uid']['id']){
                        //获取提交人nickname
                        $arr .= $item['sale_uid']['text'].'、';

                        //获取提交人部门
                        $arr .= $item['sale_org']['text'].'、';
                    }
                }
            }else{
                if ($item['get_uid'] > 0){
                    if($item['get_uid']['id']){
                        //获取提交人部门
                        $arr .= Db::table('user')
                                ->alias ('u')
                                ->join(['user_organization'=>'o'],'o.id=u.department_id')
                                ->where (['u.id'=>$item['get_uid']['id']])
                                ->value('o.title').'、';

                        //获取提交人nickname
                        $arr .= Db::table('user')
                                ->alias ('u')
                                ->where (['u.id'=>$item['get_uid']['id']])
                                ->value('u.nickname').'、';
                    }
                }
            }

            if (isset($item['certificate_characteristic'])){
                if (is_array ($item['certificate_characteristic'])){
                    $arrs['certificate_characteristic'] = $item['certificate_characteristic']['id'];
                }else{
                    $arrs['certificate_characteristic'] = $item['certificate_characteristic'];
                }
            }

            //人数
            if(isset($item['people_num'])){
                $arr .= $item['people_num'].'、';
            }

            $arrs['content'] = rtrim($arr,'、');
            $arrs['id'] = $item['id'];
            $arrs['create_date'] = $item['create_date'];
            $arrs['is_sc'] = $item['is_sc'];
            $arrs['arr'] = $item;
            $result[] = $arrs;

        }

        return $result;
    }


    /**
     * 判断用户规则是否满足
     * @return Bool
     */
    public function determineRule($cerId,$uid)
    {
        $ruleModel = new PublicSaleRule();

        $certificateSer = new Certificate();

        $certificateModel = new SystemCertificate();

        $user = new \app\model\User();
        //规则列表
        $ruleList = $ruleModel->getBetweenRule();
        $org = $user->getUserLevelInfoModel($uid);

        //用户证书$cerId 转数组
        $uidCerArr = explode(',',$cerId);

        if (!empty($ruleList)){
            $bool = 2;
            foreach ($ruleList as $item) {

                //判断规则是否生效
                if ($item['start_date'] != '0000-00-00 00:00:00' && $item['end_date'] != '0000-00-00 00:00:00'){

                    if (strtotime($item['start_date']) < time() && time() < strtotime($item['end_date'])){

                        //判断周内条件
                        if ($item['week'] > 0){
                            //如果周天等于9
                            if (date('w') == 0){
                                $day = 9;
                            }else{
                                $day = date('w');
                            }
                            if ($item['week'] != $day){
                                $bool = 1;
                                continue;
                            }else{
                                $bool = 2;
                            }
                        }
                        //判断用户证书是否存在于规则
                        $ruleCer = explode (',', $item['certificate_id']);
//                        print_r ($ruleCer);die;
                        //判断设置了几级证书
                        $cerNum = count($ruleCer);

                        if ($cerNum == 1){ //设置一级证书

                            if ($ruleCer[0] == $uidCerArr[0]){
                                //判断人员是否存在规则列表
                                if (!empty($item['uid'])){
                                    if (in_array ($uid, explode (',', $item['uid']))){
                                        $bool = 1;
                                    }
                                }

                                //判断部门是否存在规则列表
                                if (!empty($item['org_id'])){
                                    if (in_array ($org['department_id']['id'], explode (',', $item['org_id']))){
                                        $bool = 1;
                                    }
                                }
                            }else{
                                $bool = 1;
                            }
                        }elseif ($cerNum == 2){//设置二级证书
                            if ($ruleCer[0] == $uidCerArr[0]){
                                if ($ruleCer[1] == $uidCerArr[1]){
                                    //判断人员是否存在规则列表
                                    if (!empty($item['uid'])){
                                        if (in_array ($uid, explode (',', $item['uid']))){
                                            $bool = 1;
                                        }
                                    }

                                    //判断部门是否存在规则列表
                                    if (!empty($item['org_id'])){
                                        if (in_array ($org['department_id']['id'], explode (',', $item['org_id']))){
                                            $bool = 1;
                                        }
                                    }
                                }else{
                                    $bool = 1;
                                }
                            }else{
                                $bool = 1;
                            }
                        }elseif($cerNum == 3){//设置三级证书

                            if ($ruleCer[0] == $uidCerArr[0]){
                                if ($ruleCer[1] == $uidCerArr[1]){
                                    if ($ruleCer[2] == $uidCerArr[2]){
                                        //判断人员是否存在规则列表
                                        if (!empty($item['uid'])){
                                            if (in_array ($uid, explode (',', $item['uid']))){
                                                $bool = 1;
                                            }
                                        }

                                        //判断部门是否存在规则列表
                                        if (!empty($item['org_id'])){
                                            if (in_array ($org['department_id']['id'], explode (',', $item['org_id']))){
                                                $bool = 1;
                                            }
                                        }
                                    }else{
                                        $bool = 1;
                                    }
                                }else{
                                    $bool = 1;
                                }
                            }else{
                                $bool = 1;
                            }
                        }elseif ($cerNum == 4) {//设置四级证书

                            if ($ruleCer[0] == $uidCerArr[0]){
                                if ($ruleCer[1] == $uidCerArr[1]){
                                    if ($ruleCer[2] == $uidCerArr[2]){
                                        if ($ruleCer[3] == $uidCerArr[3]){

                                            //判断部门是否存在规则列表
                                            if (!empty($item['org_id'])){
                                                if (in_array ($org['department_id']['id'], explode (',', $item['org_id']))){
                                                    //判断人员是否存在规则列表
                                                    if (!empty($item['uid'])){
                                                        if (in_array ($uid, explode (',', $item['uid']))){
                                                            $bool = 1;
                                                        }else{
                                                            $bool = 2;
                                                        }
                                                    }
                                                }else{
                                                    $bool = 2;
                                                }
                                            }
                                        }else{
                                            $bool = 1;
                                        }
                                    }else{
                                        $bool = 1;
                                    }
                                }else{
                                    $bool = 1;
                                }
                            }else{
                                $bool = 1;
                            }
                        }else{
                            $bool = 1;
                        }
                    }else{
                        $bool = 1;
                    }
                }else{
                    $bool = 1;
                }
            }
        }else{
            $bool = 1;
        }


        if ($bool == 2){
            return false;
        }
        return true;
    }


    //获取公示资料
    public function getPublicyInfo($data,$uid)
    {
        $deal = new OrderDeal();

        foreach ($data as &$datum) {
            //添加业务类型
            $datum['business_id'] = ['id'=>'','text'=>'倒证'];
            $result = $deal->getsuppleInfo($datum['id'],$datum['certificate_code']);

            if (empty($result)){
                $datum['electronic_certificate_file_illustrate'] = '';
                $datum['electronic_certificate_file'] = '';
                $datum['electronic_certificate_date'] = '';
                $datum['b_electronic_certificate_file_illustrate'] = '';
                $datum['b_electronic_certificate_date'] = '';
                $datum['b_electronic_certificate_file'] = '';
                $datum['b_reg_electronic_certificate_date'] = '';
                $datum['electronic_certificate_file_illustrate_name'] = '';
                $datum['b_electronic_certificate_file_name'] = '';
                $datum['order_status'] = '';
            }else{
                $datum['electronic_certificate_file_illustrate'] = $result['electronic_certificate_file_illustrate']?:'';
                $datum['electronic_certificate_file'] = $result['electronic_certificate_file']?:'';
                $datum['electronic_certificate_date'] = $result['electronic_certificate_date']?:'';
                $datum['b_electronic_certificate_file_illustrate'] = $result['b_electronic_certificate_file_illustrate']?:'';
                $datum['b_electronic_certificate_date'] = $result['b_electronic_certificate_date']?:'';
                $datum['b_electronic_certificate_file'] = $result['b_electronic_certificate_file']?:'';
                $datum['b_reg_electronic_certificate_date'] = $result['b_reg_electronic_certificate_date']?:'';
                $datum['electronic_certificate_file_illustrate_name'] = $result['electronic_certificate_file_illustrate_name']?:'';
                $datum['b_electronic_certificate_file_name'] = $result['b_electronic_certificate_file_name']?:'';
                $datum['order_status'] = $result['order_status'];
            }
        }
        return $data;
    }

}